<?php
namespace app\api\controller;

use app\api\model\GoodsModel;
use think\Controller;
use think\Request;


class Goods extends Controller
{

    /**
     * 商品列表
     * @return \think\response\View
     *
     */
    public function lists(Request $request)
    {
        $goods = $request->post();
        if(empty($goods['bid'])) $this->outPut('', 1001, "缺少商家id");
        $fieid = 'goodsid,bid,pic,name,unit,max_price,min_price,favournum,stock,sales,explain,freight_unify';
        $bus = GoodsModel::instance()->buslist(['bid'=>$goods['bid'],'status1'=>1],'name,address');
        if(!empty($goods['tid'])){
            if(!empty($goods['ptid'])){
                $result = GoodsModel::instance()->goodslist(['menu'=>$goods['tid'],'type'=>$goods['ptid'],'status'=>1,'bid'=>$goods['bid']],$fieid);
            }else{
                $result = GoodsModel::instance()->goodslist(['menu'=>$goods['tid'],'status'=>1,'bid'=>$goods['bid']],$fieid);
            }
       }else{
           $result = GoodsModel::instance()->goodslistlimt(['recom'=>1,'status'=>1,'bid'=>$goods['bid']],$fieid);
            $offer = GoodsModel::instance()->specialOffer(['status'=>1,'bid'=>$goods['bid'],'special_offer'=>1],$fieid);
	foreach ($offer as &$va){
                $va['pic'] = uploadpath('goods',$va['pic']);
            }
            $bus['offer'] = $offer;
        }
        foreach ($result as $key=>&$val){
            $val['pic'] = uploadpath('goods',$val['pic']);
        }
        $bus['goods'] = $result;
        $this->jsonOut($bus);

    }
    /**
     * 商品详情
     */
    public function detail(Request $request){
        $goodsid = $request->get('goodsid');
        if(empty($goodsid)) $this->outPut(null,1001,"缺少商品id");
        
        $where['goodsid'] = $goodsid;
        //$where['status'] = 1;
        $field = 'goodsid,bid,pic,name,unit,max_price,min_price,favournum,stock,sales,explain,content,status,pics,weight,freight_unify,open_rule';
        $result = GoodsModel::instance()->getDetail($where,$field);

        $result['pic'] = uploadpath('goods',$result['pic']);

        $this->jsonOut($result);
        
    }
/**
     * 搜索
     */
    public function search(Request $request){
        $title = $request->post();
        $fieid = 'goodsid,bid,pic,name,unit,max_price,min_price,favournum,stock,sales,explain,freight_unify';
        $result = GoodsModel::instance()->search($title,$fieid);
        foreach ($result as &$val){
            $val['pic'] = uploadpath('goods',$val['pic']);
        }
        $this->jsonOut($result);
    }
   /**
     * 热销 推荐 积分 商品
     * 0:积分商品  1：特价   2：热销
     */
    public function goodsclassify(Request $request){
        $goods = $request->post();
        if(empty($goods['bid'])) $this->outPut('', 1001, "缺少商家id");
        //if($goods['type']) $this->outPut('', 1001, "type");
        $fieid = 'goodsid,bid,pic,name,unit,max_price,min_price,favournum,stock,sales,explain,freight_unify,need_integral';
        $result = GoodsModel::instance()->goodsclassify($goods,$fieid);
        foreach ($result as &$val){
            $val['pic'] = uploadpath('goods',$val['pic']);
        }
        $this->jsonOut($result);
    }
/**
     * 新闻列表
     */
    public function newslist(Request $request){
        $bid = $request->get('bid');
        $result = GoodsModel::instance()->newslist($bid);
        $this->jsonOut($result);
    }
    /**
     * 新闻详情
     */
    public function newsdetails(Request $request){
        $neid = $request->get('neid');
        $result = GoodsModel::instance()->newsdetails($neid);
        $this->jsonOut($result);
    }
        
}
